package com.wmx.java_8.collector;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;

/**********************************
 * @author wmx
 * @date 2019/10/15 16:27
 * @description:
 * @version 1.0
 ************************************/
public class CollectorsDemo {
    public static void main(String[] args) {
        Employee zhangsan = new Employee(1L, "张三", "蚂蚁金服", 45, true);
        Employee lisi = new Employee(3L, "李四", "京东", 23, false);
        Employee wangwu = new Employee(3L, "王五", "百度", 23, false);
        Employee qiaofeng = new Employee(6L, "乔峰", "金庸", 33, true);
        Employee xuzhu = new Employee(7L, null, "金庸", 45, true);
        Employee duanyu = new Employee(8L, "段誉", "金庸", 66, true);

        List<Employee> employees = Arrays.asList(zhangsan, lisi, wangwu, qiaofeng, xuzhu, duanyu);
        List<Employee> toList = employees.stream().collect(toList());
        System.out.println("Collects.toList()转化为list" + toList);

        //map id Employee
      /*  Map<Long, Employee> integerEmployeeMap = employees.stream().collect(Collectors.toMap(Employee::getId, employee -> employee));
        integerEmployeeMap.forEach((k, v) -> {
            System.out.println("key值：" + k + "   v值：" + v);
        });*/

        // 解决key重复问题
        Map<Long, String> memberMap = employees.stream().collect(HashMap::new, (m, v) ->
                m.put(v.getId(), v.getName() == null ? "" : v.getName()), HashMap::putAll);
        memberMap.forEach((k, v) -> {
            if ("".equals(v)) {
                System.out.println("key值：" + k + "   v值：" + v);
            }

        });


        //id 和 age 属性 去重
        System.out.printf("===========根据Id数据去重==============");
        List<Employee> removeRepetitionId = employees.stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(Comparator.comparing(v -> v.getId()+ ";" + v.getAge()))), ArrayList::new)
        );

        removeRepetitionId.forEach(System.out::println);


        //去重
        List<Employee> distinctEmployee = employees.stream().distinct().collect(toList());
        System.out.println("去重：" + distinctEmployee.toString());

    }

}
